﻿using System;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Main_Inventory_InventoryOption : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string strsql = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            //ScriptManager.RegisterStartupScript(Page, this.GetType(), "redirect", "window.open('../../login.aspx','_parent');", true);
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {            
            if (chkBatch.Checked == false)
            {
                panelBatch.Enabled = false;                
            }
            Bind_Trans_Settings();
            Bind_Features();
            Bind_RoundOff();
            Bind_Alerts();
            Bind_Items();
            Bind_TMaintaion();
        }
    }
    protected void Bind_TMaintaion()
    {
        strsql = "Select * from ACC_COMPANY where CompID=1";
        DataTable DTcompany = DU.getdata(strsql);
        if (DTcompany.Rows.Count > 0)
        {
            if (DTcompany.Rows[0]["IsAccEnabled"].ToString() == "True")
            {
                chkaccount.Checked = true;
            }
            if (DTcompany.Rows[0]["IsInvrEnabled"].ToString() == "True")
            {
                chkinventory.Checked = true;
            }
            if (DTcompany.Rows[0]["IsInvcEnabled"].ToString() == "True")
            {
                chkinvoicing.Checked = true;
            }
        }
    }
    protected void chkBatch_CheckedChanged(object sender, EventArgs e)
    {
        if (chkBatch.Checked == true)
        {
            panelBatch.Enabled = true;
        }
        if (chkBatch.Checked == false)
        {
            panelBatch.Enabled = false;
            chkExpiryDate.Checked = false;
            chkWarranty.Checked = false;
            chkActualBatch.Checked = false;        
        }
    }
    protected void radioAutomatic_CheckedChanged(object sender, EventArgs e)
    {
        if (radioAutomatic.Checked == true)
        {
            panelAllocation.Visible = true;
            lblAutometic.Visible = true;
            lblManual.Visible = false;
        }       

    }
    protected void radioManual_CheckedChanged(object sender, EventArgs e)
    {
        if (radioManual.Checked == true)
        {
            panelAllocation.Visible = false;
            lblAutometic.Visible = false;
            lblManual.Visible = true;
        }
       
    }
    protected void chkEnableBOM_CheckedChanged(object sender, EventArgs e)
    {
        if (chkEnableBOM.Checked == true)
        {
            PanelBOM.Enabled = true;
        }
        else if (chkEnableBOM.Checked == false)
        {
            chkAutoJrnlSale.Checked = false;
            PanelBOM.Enabled = false;

        }

    }
    protected void chkEnableTimber_CheckedChanged(object sender, EventArgs e)
    {

    }
    /// <summary>
    /// Bind Check Box from Database
    /// From IVR_TRANSETTINGS Table
    /// To Transaction Setting Tab, GL Allocations Tab
    /// </summary>
    protected void Bind_Trans_Settings()
    {
        strsql = "select * from IVR_TRANSETTINGS";
        DataTable DTBind = DU.getdata(strsql);
        if (DTBind.Rows.Count > 0)
        {
            if (DTBind.Rows[0]["DCIN"].ToString() == "True")
            {
                chkDCInward.Checked = true;
            }
            if (DTBind.Rows[0]["PURCRETURN"].ToString() == "True")
            {
                chkPurReturn.Checked = true;
            }
            if (DTBind.Rows[0]["DCOUT"].ToString() == "True")
            {
                chkDCOutward.Checked = true;
            }
            if (DTBind.Rows[0]["SALERETURN"].ToString() == "True")
            {
                chkSaleReturn.Checked = true;
            }
            if (DTBind.Rows[0]["STOCKJRNL"].ToString() == "True")
            {
                chkJournal.Checked = true;
            }
            if (DTBind.Rows[0]["SEPEFFECTIVEDATE"].ToString() == "True")
            {
                chkSeparateDate.Checked = true;
            }
            if (DTBind.Rows[0]["ALLOWZEROAMOUNT"].ToString() == "True")
            {
                chkAllowZeroAmt.Checked = true;
            }
            if (DTBind.Rows[0]["PURCLEDGERALLOCOPT"].ToString() == "0")
            {
                radioPurTax.Checked = true;
            }
            else
            {
                radioPurItem.Checked = true;
            }
            if (DTBind.Rows[0]["SALELEDGERALLOCOPT"].ToString() == "0")
            {
                radioSaleTax.Checked = true;
            }
            else
            {
                radioSaleItem.Checked = true;
            }
            if (DTBind.Rows[0]["GLALLOCMETHOD"].ToString() == "0")
            {
                radioAutomatic.Checked = true;
                panelAllocation.Visible = true;
                lblAutometic.Visible = true;
                lblManual.Visible = false;
            }
            else
            {
                radioManual.Checked = true;
                panelAllocation.Visible = false;
                lblAutometic.Visible = false;
                lblManual.Visible = true;
            }
        }
    }
    /// <summary>
    /// Bind Check Box from Database
    /// From IVR_Settings Table
    /// To Feature Tab, Batch Tab, Edit Caption Tab, Price Group Tab, Valuation Method Tab
    /// </summary> 
    protected void Bind_Features()
    {
        int ValuationMethod = 0;
        int PriceGroup = 0;
        strsql = "select * from IVR_SETTINGS";
        DataTable DTBind = DU.getdata(strsql);
        if (DTBind.Rows.Count > 0)
        {
            if (DTBind.Rows[0]["WHSMULTI"].ToString() == "True")
            {
                chkEnableLocation.Checked = true;
            }            
            if (DTBind.Rows[0]["BATACTIVE"].ToString() == "True")
            {
                chkBatch.Checked = true;
                panelBatch.Enabled = true;
            }
            if (DTBind.Rows[0]["BATEXPIRYDATE"].ToString() == "True")
            {
                chkExpiryDate.Checked = true;
            }
            if (DTBind.Rows[0]["BATWARRANTY"].ToString() == "True")
            {
                chkWarranty.Checked = true;
            }
            if (DTBind.Rows[0]["BATACTUALVALUATN"].ToString() == "True")
            {
                chkActualBatch.Checked = true;
            }            
            if (DTBind.Rows[0]["OTHAUTOPORTBS"].ToString() == "True")
            {
                chkUpdateStock.Checked = true;
            }
            if (DTBind.Rows[0]["OTHBOM"].ToString() == "True")
            {
                chkEnableBOM.Checked = true;
                PanelBOM.Enabled = true;
            }
            if (DTBind.Rows[0]["OTHVATERETURN"].ToString() == "True")
            {
                chkEnableVAT.Checked = true;
            }            
            if (DTBind.Rows[0]["AUTOJRNL"].ToString() == "True")
            {
                chkAutoJrnlSale.Checked = true;
            }
            if (DTBind.Rows[0]["TCS"].ToString() == "True")
            {
                chkEnableTCS.Checked = true;
            }
            if (DTBind.Rows[0]["TIMBER"].ToString() == "True")
            {
                chkEnableTimber.Checked = true;
            }
            if (DTBind.Rows[0]["VMDOPTION"].ToString() == "0")
            {
               radioFIFO.Checked = true;
            }
            else if (DTBind.Rows[0]["VMDOPTION"].ToString() == "1")
            {
                radioLIFO.Checked = true;
            }
            else if (DTBind.Rows[0]["VMDOPTION"].ToString() == "2")
            {
                radioAverage.Checked = true;
            }
            else if (DTBind.Rows[0]["VMDOPTION"].ToString() == "3")
            {
               radioActual.Checked = true ;
            }
            if (DTBind.Rows[0]["SPGOPTION"].ToString() == "0")
            {
                radioNone.Checked = true;
            }
            else if (DTBind.Rows[0]["SPGOPTION"].ToString() == "1")
            {
                radioDiscount.Checked = true;
            }
            else if (DTBind.Rows[0]["SPGOPTION"].ToString() == "2")
            {
                radioItemPricing.Checked = true;
            }
            txtCaptionBatch.Text = DTBind.Rows[0]["BATCAP"].ToString();
            txtCaptionLocation.Text = DTBind.Rows[0]["WHSCAP"].ToString();
            txtCaptionSales.Text = DTBind.Rows[0]["OTHSALEPERSONCAP"].ToString();
        }
    }
    /// <summary>
    /// Update IVR_TranSettings Database Table
    /// From USP_IVR_SaveTranSetting Stored Procedure
    /// Data Updated From "Transactions Settings Tab"
    /// </summary>   
    protected void Insert_Tran_Setting()
    {
        int PurAllocation=0;
        int SaleAllocation=0;
        int GAllocation = 0;
        if (radioPurTax.Checked == true)
        {
            PurAllocation = 0;
        }
        else if (radioPurItem.Checked == true)
        {
            PurAllocation = 1;
        }
        if (radioSaleTax.Checked == true)
        {
            SaleAllocation = 0;
        }
        else if (radioSaleItem.Checked == true)
        {
            SaleAllocation = 1;
        }
        if (radioAutomatic.Checked == true)
        {
            GAllocation = 0;
        }
        else if (radioManual.Checked == true)
        {
            GAllocation = 1;
        }

        string spname = "USP_IVR_SaveTranSetting";
        SqlParameter[] sparam =
                                {
                                    new SqlParameter("@DCIN",chkDCInward.Checked),
                                    new SqlParameter("@PURCRETURN",chkPurReturn.Checked),
                                    new SqlParameter("@DCOUT",chkDCOutward.Checked),
                                    new SqlParameter("@SALERETURN",chkSaleReturn.Checked),
                                    new SqlParameter("@STOCKJRNL",chkJournal.Checked),
                                    new SqlParameter("@SEPEFFECTIVEDATE",chkSeparateDate.Checked),
                                    new SqlParameter("@ALLOWZEROAMOUNT",chkAllowZeroAmt.Checked),
                                    new SqlParameter("@PURCLEDGERALLOCOPT",PurAllocation.ToString()),
                                    new SqlParameter("@SALELEDGERALLOCOPT",SaleAllocation.ToString()),
                                    new SqlParameter("@GLALLOCMETHOD",GAllocation.ToString())
                                };
        int result = DU.executeprocedure(spname, sparam);       
    }
    /// <summary>
    /// Update IVR_TRANSTYPE Database Table
    /// From USP_UPD_IVR_TransType Stored Procedure
    /// Data Updated From "Transactions Settings Tab"
    /// </summary>    
    protected void Update_TransType()
    {
        string spname = "USP_UPD_IVR_TransType";
        SqlParameter[] sparam =
                            {
                                new SqlParameter ("@ID","2"),
                                new SqlParameter("@Active",chkDCOutward.Checked)
                            };
        int result = DU.executeprocedure(spname, sparam);
        SqlParameter[] sparam1 =
                            {
                                new SqlParameter ("@ID","3"),
                                new SqlParameter("@Active",chkSaleReturn.Checked)
                            };
        int result1 = DU.executeprocedure(spname, sparam1);
        SqlParameter[] sparam2 =
                            {
                                new SqlParameter ("@ID","5"),
                                new SqlParameter("@Active",chkDCInward.Checked)
                            };
        int result2 = DU.executeprocedure(spname, sparam2);
        SqlParameter[] sparam3 =
                            {
                                new SqlParameter ("@ID","6"),
                                new SqlParameter("@Active",chkPurReturn.Checked)
                            };
        int result3 = DU.executeprocedure(spname, sparam3);
        SqlParameter[] sparam4 =
                            {
                                new SqlParameter ("@ID","7"),
                                new SqlParameter("@Active",chkJournal.Checked)
                            };
        int result4 = DU.executeprocedure(spname, sparam4);

        SqlParameter[] sparam5 =
                            {
                                new SqlParameter ("@ID","8"),
                                new SqlParameter("@Active",chkPurchaseOrder.Checked)
                            };
        int result5 = DU.executeprocedure(spname, sparam5);
        SqlParameter[] sparam6 =
                            {
                                new SqlParameter ("@ID","9"),
                                new SqlParameter("@Active",chkSalesOrder.Checked)
                            };
        int result6 = DU.executeprocedure(spname, sparam6); 


    }
    //for manage tab
    protected void Update_manage()
    {
        string tempaccount = string.Empty; string tempinventory = string.Empty; string tempinvoice = string.Empty;
        if (chkaccount.Checked == true)
        {tempaccount = "1";}
        else
        {tempaccount="0"; }
        if (chkinventory.Checked == true)
        { tempinventory = "1"; }
        else
        {tempinventory="0"; }
        if (chkinvoicing.Checked == true)
        { tempinvoice = "1"; }
        else
        { tempinvoice = "0"; }
        string sqlupdatestr = "Update Acc_Company set IsAccEnabled='"+tempaccount+"',IsInvcEnabled='"+tempinvoice+"',IsInvrEnabled='"+tempinventory+"' where CompId=1";

        int insert= DU.executesql(sqlupdatestr);       
    }
    /// <summary>
    /// Save All Updation 
    /// </summary>    
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
        
            Insert_Tran_Setting();
            Update_TransType();
            Update_manage();
            Insert_GeneralSettings();
            Update_RoundOff();
            Update_Alerts();
            Insert_ItemSettings();                      
            string msg ="Update Successfully!";
            AlertBox(msg);           
        }
        catch (Exception ex)
        {
            string msg = ex.Message;
            AlertBox(msg);
        }
    }   
    //Cancel Transactions Settings & Redirect To WelCome.aspx Page
    protected void btnClose_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Welcome.aspx");
    }
    /// <summary>
    /// Update IVR_SETTINGS Database Table
    /// From USP_IVR_SaveGeneralSetting Stored Procedure
    /// Data Inserted from "Features Tab,Price Group Tab,Batch Tab, Edit Caption Tab & Valuation Method Tab"
    /// </summary>
    protected void Insert_GeneralSettings()
    {
        int ValuationMethod = 0;
        int PriceGroup = 0;
        
        if (radioFIFO.Checked == true)
        {
            ValuationMethod = 0;
        }
        else if (radioLIFO.Checked == true)
        {
            ValuationMethod = 1;
        }
        else if (radioAverage.Checked == true)
        {
            ValuationMethod = 2; 
        }
        else if (radioActual.Checked == true)
        {
            ValuationMethod = 3;
        }
        
        if (radioNone.Checked == true)
        {
            PriceGroup = 0;
        }
        else if (radioDiscount.Checked == true)
        {
            PriceGroup = 1;
        }
        else if (radioItemPricing.Checked == true)
        {
            PriceGroup = 2;
        }
        if (txtCaptionSales.Text.Trim() == "")
        {
            txtCaptionSales.Text = "Sales Person";
        }
        SqlParameter[] sparam = {
                                    new SqlParameter("@WHSMULTI",chkEnableLocation.Checked),
                                    new SqlParameter("@WHSCAP", txtCaptionLocation.Text.Trim()),
                                    new SqlParameter("@SPGOPTION",PriceGroup.ToString()),
                                    new SqlParameter("@VMDOPTION",ValuationMethod.ToString()),
                                    new SqlParameter("@BATACTIVE", chkBatch.Checked),
                                    new SqlParameter("@BATEXPIRYDATE",chkExpiryDate.Checked),
                                    new SqlParameter("@BATWARRANTY",chkWarranty.Checked),
                                    new SqlParameter("@BATACTUALVALUATN",chkActualBatch.Checked),
                                    new SqlParameter("@OTHSALEPERSONCAP",txtCaptionSales.Text.Trim()),
                                    new SqlParameter("@OTHAUTOPORTBS",chkUpdateStock.Checked),
                                    new SqlParameter("@OTHBOM",chkEnableBOM.Checked),
                                    new SqlParameter("@OTHVATERETURN",chkEnableVAT.Checked),
                                    new SqlParameter("@BATCAP",txtCaptionBatch.Text.Trim()),
                                    new SqlParameter("@AUTOJRNL",chkAutoJrnlSale.Checked),
                                    new SqlParameter("@TCS",chkEnableTCS.Checked),
                                    new SqlParameter("@TIMBER",chkEnableTimber.Checked)                                    
                                };
        int result = DU.executeprocedure("USP_IVR_SaveGeneralSetting", sparam);
    }
    /// <summary>
    /// Bind Combo Box from Database
    /// From IVR_ROUNDOFFSETTINGS Table
    /// To Round Off Tab
    /// </summary>
    protected void Bind_RoundOff()
    {
        strsql = "Select * From IVR_ROUNDOFFSETTINGS";
        DataTable DTBind = DU.getdata(strsql);
        if (DTBind.Rows.Count > 0)
        {
            ddlRoundVat.SelectedValue = DTBind.Rows[0]["ROFOPTION"].ToString();
            ddlRoundCST.SelectedValue = DTBind.Rows[1]["ROFOPTION"].ToString();
            ddlRoundTCS.SelectedValue = DTBind.Rows[2]["ROFOPTION"].ToString();
            ddlRoundAmtRec.SelectedValue = DTBind.Rows[3]["ROFOPTION"].ToString();
        }
    }
    /// <summary>
    /// Update IVR_ROUNDOFFSETTINGS Database Table
    /// From USP_UPD_IVR_ROUNDOFFSETTINGS Stored Procedure
    /// Data Inserted from "Round Off Tab"
    /// </summary>
    protected void Update_RoundOff()
    {

        string spname = "USP_UPD_IVR_ROUNDOFFSETTINGS";
        SqlParameter[] sparam =
                            {
                                new SqlParameter ("@ROFID","1"),
                                new SqlParameter("@ROFOPTION",ddlRoundVat.SelectedValue)
                            };
        int result = DU.executeprocedure(spname, sparam);
        SqlParameter[] sparam1 =
                            {
                                new SqlParameter ("@ROFID","2"),
                                new SqlParameter("@ROFOPTION",ddlRoundCST.SelectedValue)
                            };
        int result1 = DU.executeprocedure(spname, sparam1);
        SqlParameter[] sparam2 =
                            {
                                new SqlParameter ("@ROFID","3"),
                                new SqlParameter("@ROFOPTION",ddlRoundTCS.SelectedValue)
                            };
        int result2 = DU.executeprocedure(spname, sparam2);
        SqlParameter[] sparam3 =
                            {
                                new SqlParameter ("@ROFID","4"),
                                new SqlParameter("@ROFOPTION", ddlRoundAmtRec.SelectedValue)
                            };
        int result3 = DU.executeprocedure(spname, sparam3);
    }
    /// <summary>
    /// Bind Combo Box from Database
    /// From IVR_WARNINGSETTINGS Table
    /// To "Alert Tab"
    /// </summary>
    protected void Bind_Alerts()
    {
        strsql = "Select * From IVR_WARNINGSETTINGS";
        DataTable DTBind = DU.getdata(strsql);
        if (DTBind.Rows.Count > 0)
        {
            ddlAlertNegative.SelectedValue = DTBind.Rows[0]["WRNOPTION"].ToString();
            ddlAlertMinLevel.SelectedValue = DTBind.Rows[1]["WRNOPTION"].ToString();
            ddlAlertReorderLevel.SelectedValue = DTBind.Rows[2]["WRNOPTION"].ToString();
            ddlAlertMaxLevel.SelectedValue = DTBind.Rows[3]["WRNOPTION"].ToString();
            ddlAlertTIN.SelectedValue = DTBind.Rows[4]["WRNOPTION"].ToString();
            ddlAlertCST.SelectedValue = DTBind.Rows[5]["WRNOPTION"].ToString();
            ddlAlertBatch.SelectedValue = DTBind.Rows[6]["WRNOPTION"].ToString();
            ddlAlertCredit.SelectedValue = DTBind.Rows[7]["WRNOPTION"].ToString();
        }
    }
    /// <summary>
    /// Update IVR_WARNINGSETTINGS Database Table
    /// From USP_UPD_IVR_WARNINGSETTINGS Stored Procedure
    /// Data Inserted from "Alert Tab"
    /// </summary>
    protected void Update_Alerts()
    {

        string spname = "USP_UPD_IVR_WARNINGSETTINGS";
        SqlParameter[] sparam =
                            {
                                new SqlParameter ("@WRNID","1"),
                                new SqlParameter("@WRNOPTION",ddlAlertNegative.SelectedValue)
                            };
        int result = DU.executeprocedure(spname, sparam);
        SqlParameter[] sparam1 =
                            {
                                new SqlParameter ("@WRNID","2"),
                                new SqlParameter("@WRNOPTION",ddlAlertMinLevel.SelectedValue)
                            };
        int result1 = DU.executeprocedure(spname, sparam1);
        SqlParameter[] sparam2 =
                            {
                                new SqlParameter ("@WRNID","3"),
                                new SqlParameter("@WRNOPTION",ddlAlertReorderLevel.SelectedValue)
                            };
        int result2 = DU.executeprocedure(spname, sparam2);
        SqlParameter[] sparam3 =
                            {
                                new SqlParameter ("@WRNID","4"),
                                new SqlParameter("@WRNOPTION", ddlAlertMaxLevel.SelectedValue)
                            };
        int result3 = DU.executeprocedure(spname, sparam3);
        SqlParameter[] sparam4 =
                            {
                                new SqlParameter ("@WRNID","5"),
                                new SqlParameter("@WRNOPTION",ddlAlertTIN.SelectedValue)
                            };
        int result4 = DU.executeprocedure(spname, sparam4);
        SqlParameter[] sparam5 =
                            {
                                new SqlParameter ("@WRNID","6"),
                                new SqlParameter("@WRNOPTION",ddlAlertCST.SelectedValue)
                            };
        int result15 = DU.executeprocedure(spname, sparam5);
        SqlParameter[] sparam6 =
                            {
                                new SqlParameter ("@WRNID","7"),
                                new SqlParameter("@WRNOPTION",ddlAlertBatch.SelectedValue)
                            };
        int result6 = DU.executeprocedure(spname, sparam6);
        SqlParameter[] sparam7 =
                            {
                                new SqlParameter ("@WRNID","8"),
                                new SqlParameter("@WRNOPTION", ddlAlertCredit.SelectedValue)
                            };
        int result7 = DU.executeprocedure(spname, sparam7);
    }
    /// <summary>
    /// Bind Check Box from Database
    /// From IVR_ITEMSETTINGS Table
    /// To "Items Tab"
    /// </summary>
    protected void Bind_Items()
    {
        strsql = "select * from IVR_ITEMSETTINGS";
        DataTable DTBind = DU.getdata(strsql);
        if (DTBind.Rows.Count > 0)
        {
            if (DTBind.Rows[0]["ITMCODE"].ToString() == "True")
            {
                chkItemCode.Checked = true;
            }
            if (DTBind.Rows[0]["ITMALTUNIT"].ToString() == "True")
            {
                chkAltUnitItem.Checked = true;
            }
            if (DTBind.Rows[0]["ITMUPCCODE"].ToString() == "True")
            {
               chkUPS.Checked = true;
            }
            if (DTBind.Rows[0]["ITMUPCCODE"].ToString() == "True")
            {
                chkAddDescription.Checked = true;
            }
            if (DTBind.Rows[0]["ITMALTDESC"].ToString() == "True")
            {
                chkAltDescription .Checked = true;
            }
            if (DTBind.Rows[0]["ITMREMARKS"].ToString() == "True")
            {
                chkRemark.Checked = true;
            }
            if (DTBind.Rows[0]["ITMBINREF"].ToString() == "True")
            {
               chkBinReference.Checked = true;
            }
        }
    }
    /// <summary>
    /// Update IVR_ITEMSETTINGS Database Table
    /// From USP_IVR_SaveItemSettings Stored Procedure
    /// Data Inserted from "Item Tab"
    /// </summary>
    protected void Insert_ItemSettings()
    {
        string spname = "USP_IVR_SaveItemSettings";
        SqlParameter[] sparam =
                                {
                                    new SqlParameter("@ITMCODE",chkItemCode.Checked),
                                    new SqlParameter("@ITMALTUNIT",chkAltUnitItem.Checked),
                                    new SqlParameter("@ITMUPCCODE",chkUPS.Checked),
                                    new SqlParameter("@ITMADDITIONALDESC",chkAddDescription.Checked),
                                    new SqlParameter("@ITMALTDESC",chkAltDescription .Checked),
                                    new SqlParameter("@ITMREMARKS",chkRemark.Checked),
                                    new SqlParameter("@ITMBINREF",chkBinReference.Checked)                                    
                                };
        int result = DU.executeprocedure(spname, sparam);

    }
    protected void AlertBox(string Message)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "alert('" + Message + "');", true);
    }

}
                    
                  
